#include <stropts.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HPUX
#include <sys/diskio.h>
#else
#include <linux/fs.h>
#endif

int main(int argc, char **argv) 
{
#ifdef HPUX
disk_describe_type diskinfo;
#else
unsigned int diskinfo;
#endif
struct stat sbuf;

	if(argc!=2) {
#ifndef LINUX
		fprintf(stderr,"Error: Usage is 'get-devblk-size rawdev'\n");
#else
		fprintf(stderr,"Error: Usage is 'get-devblk-size blockdev'\n");
#endif
		exit(1);
	}
	int rc=stat(argv[1],&sbuf);
	if(rc==-1) {
		fprintf(stderr,"Error: Unable to stat device '%s'.\n",argv[1]);
		exit(1);
	}
#ifdef LINUX
	if(!S_ISBLK(sbuf.st_mode)) {
		fprintf(stderr,"Error: Specified name is not a block device.\n");
		exit(1);
	}
#else
	if(!S_ISCHR(sbuf.st_mode)) {
		fprintf(stderr,"Error: Specified name is not a character/raw device.\n");
		exit(1);
	}
#endif
	int fd=open(argv[1],O_RDWR,0);
	if(fd==-1) {
		fprintf(stderr,"Error: Unable to open device for reading.\n");
		exit(1);
	}
#ifndef LINUX
	int x=ioctl(fd,IOC_OUT|DIOC_DESCRIBE,&diskinfo);
#else
	int x=ioctl(fd,IOC_OUT|BLKBSZGET,&diskinfo);
#endif
	if(x==-1) {
		fprintf(stderr,"Error: Unable to query device characteristics.\n");
		exit(1);
	}
#ifdef HPUX
	printf("%d\n",diskinfo.lgblksz);
#else
	printf("%d\n",diskinfo);
#endif
	exit(0);
}

